$(document).ready(function(){
    //var postUrl = "http://59.110.114.107:8888/";
    var postUrl = "http://59.110.114.107:8888/";
    // -----------------金融注册

    $("#fi-regist").click(function(){
        // var regin =$("#regin").val().substr(0,1);
        if(!window.localStorage){
            console.log("浏览器不支持localstorage");
        }else{
            var bkName = $("#bankName").val();
            var fkUserName = $("#fkUserName").val()
            // alert(bkName);
            window.localStorage.setItem("bankName", bkName);
            window.localStorage.setItem("fiuserName", fkUserName);
        }
        if($('#checkbox-id').is(':checked')){
            $.ajax({
                type : "POST",
                url:postUrl+"government/checkSendCode",
                data:{
                    phoneNo:$("#contactPhone").val(),
                    messageCode:$("#messageCode").val()
                },
                success:function(result){
                    debugger
                    if(result.meta.success ){
                        var data={
                            bankId:$("#bankId").val(),
                            bankName:$("#bankName").val(),
                            contactName:$("#contactName").val(),
                            contactPhone:$("#contactPhone").val(),
                            fkUserName:$("#fkUserName").val(),
                            password:$("#loginpwd").val(),
                            regin:$("#regin option:selected").val(),
                            street:$("#street").val()
                        };
                        $("#bankDesc").val() ? data.description= $("#bankDesc").val():null;
                        $.ajax({
                            type : "POST",
                            url:postUrl+"bank/register",
                            data:data,
                            success:function(result){

                                var form = $(".form-horizontal");
                                $(form)[0].reset();
                                if(result.meta.success ){
                                    window.location.href = "financialInstitution.html";
                                }else{
                                    alert(result.meta.message);
                                }

                            }
                        });
                    }else{
                        alert("验证码错误");
                    }
                },
                error:function(){
                    alert("错误");
                }
            });

        }else{
            alert('请认真阅读用户注册协议，并勾选“我接受用户服务协议”');
        }

    });

    //    ----------------------------------企业注册

    $("#co-regist").click(function(){

        //alert($("#companycode").val().length);
        if($('#checkbox-id').is(':checked')){
            if($("#companycode").val().length == 18){
                debugger
                $.ajax({
                    type : "POST",
                    url:postUrl+"government/checkSendCode",
                    data:{
                        phoneNo:$("#ownerno").val(),
                        messageCode:$("#messageCode").val()
                    },
                    success:function(result){
                        debugger
                        if(result.meta.success ){
                            $.ajax({
                                type : "POST",
                                url:postUrl+"enterprise/register",
                                data:{
                                    available:$("#availableCredit").val(),
                                    businessScop:$("#businessScope").val(),
                                    contactName:$("#username").val(),
                                    contactPhone:$("#phoneno").val(),
                                    enterpriseName:$("#pci_namecn").val(),
                                    establishAddress:$("#registAddress").val(),
                                    establishTime:$("#establishmentTime").val(),
                                    fundQuota:$("#fundQuota").val(),
                                    ownerName:$("#ownername").val(),
                                    ownerPhone:$("#ownerno").val(),
                                    password:$("#loginpwd").val(),
                                    region:$("#pci_branchno option:selected").val(),
                                    registeredCaptial:$("#companyRegisterNo").val(),
                                    socialCode:$("#companycode").val(),
                                    street:$("#pci_branchno_town").val(),
                                    enterpriseDesc:$("#enterpriseDesc").val(),
                                    longAndLat:$("#longAndLat").val()

                                },
                                success:function(result){
                                    var comUsername = $("#companycode").val();
                                    var form = $(".form-horizontal");
                                    $(form)[0].reset();
                                    if(result.meta.success ){
                                        window.localStorage.setItem("comUsername", comUsername);
                                        location.href = "company.html";
                                    }else{
                                        alert(result.meta.message);
                                    }
                                },
                                error:function(){
                                    alert(result.meta.message);
                                }
                            });
                        }else{
                            alert("验证码错误");
                        }
                    },
                    error:function(){
                        alert("错误");
                    }
                });

            }else{
                alert("统一社会信用代码长度不满足18位，请仔细检查后重新填写！");
            }

        }else{
            alert('请认真阅读用户注册协议，并勾选“我接受用户服务协议”');
        }

    });

    $("#affirmpwd").blur(function(){
        var sepasw = $("#affirmpwd").val();
        var pasw = $("#loginpwd").val();
        if(sepasw != pasw){
            alert("两次密码不同，请确认密码！");
        }
      });

    //    ----------------------------------政府注册

    $("#go-regist").click(function(){
        var userName = $("#gousername").val();
        window.localStorage.setItem("gousername", userName);
        var fkUserName=$("#fkUserName").val();
        if($('#checkbox-id').is(':checked')){
            $.ajax({
                type : "POST",
                url:postUrl+"government/checkSendCode",
                data:{
                    phoneNo:$("#principalPhone").val(),
                    messageCode:$("#messageCode-go").val()
                },
                success:function(result){
                    if(!result.meta.success){
                        alert("验证码错误!")
                        return false;
                    }
                    $.ajax({
                        type : "POST",
                        url:postUrl+"government/register",
                        data:{
                            departmentName:$("#departmentName").val(),
                            fkUserName:fkUserName,
                            govrenmentCode:$("#govrenmentCode").val(),
                            password:$("#loginpwd").val(),
                            principalName:$("#principalName").val(),
                            principalPhone:$("#principalPhone").val()
                        },
                        success:function(result){

                            var form = $(".form-horizontal");
                            $(form)[0].reset();
                            if(result.meta.success ){
                                window.localStorage.setItem("gousername",fkUserName)
                                window.location.href = "government.html";

                            }else{
                                alert(result.meta.message);
                            }
                        },
                        error:function(){
                            alert("错误");
                        }
                    });
                }
            });


        }else{
            alert('请认真阅读用户注册协议，并勾选“我接受用户服务协议”');
        }

    });
    //企业-发送验证码
    $("#submitCode").click(function(){
        var phoneNo = $("#ownerno").val();
        //联系电话
        if(phoneNo == ''||phoneNo == null||phoneNo == undefined){
            alert('请输入法人手机号');
            return false;
        }else if(!/^1(3|4|5|7|8)\d{9}$/.test(phoneNo)){
            alert('请填写正确的法人手机号');
            return false;
        }
        time();
        $.ajax({
            type : "POST",
            url:postUrl+"government/sendCode",
            data:{
                phoneNo:phoneNo
            },
            success:function(result){
                if(result.meta.success ){
                    alert(result.meta.message);
                }else{
                    alert(result.meta.message);
                }
            }
        });


    });
    //金融机构-发送验证码
    $("#submitCode-fi").click(function(){
        var phoneNo = $("#contactPhone").val();
        //联系电话
        if(phoneNo == ''||phoneNo == null||phoneNo == undefined){
            alert('请输入联系人手机号');
            return false;
        }else if(!/^1(3|4|5|7|8)\d{9}$/.test(phoneNo)){
            alert('请填写正确的联系人手机号');
            return false;
        }
        time();
        $.ajax({
            type : "POST",
            url:postUrl+"government/sendCode",
            data:{
                phoneNo:phoneNo
            },
            success:function(result){
                if(result.meta.success ){
                    alert(result.meta.message);
                }else{
                    alert(result.meta.message);
                }
            }
        });


    });

    //政府-发送验证码
    $("#submitCode-go").click(function(){
        debugger
        var phoneNo = $("#principalPhone").val();
        //联系电话
        if(phoneNo == ''||phoneNo == null||phoneNo == undefined){
            alert('请输入政府部门负责人手机号');
            return false;
        }else if(!/^1(3|4|5|7|8)\d{9}$/.test(phoneNo)){
            alert('请填写正确的政府部门负责人手机号');
            return false;
        }
        time();
        $.ajax({
            type : "POST",
            url:postUrl+"government/sendCode",
            data:{
                phoneNo:phoneNo
            },
            success:function(result){
                if(result.meta.success ){
                    alert(result.meta.message);
                }else{
                    alert(result.meta.message);
                }
            }
        });


    });

    $("#toBDMap").click(function(){
        console.log("toBDMap")
        window.open("http://api.map.baidu.com/lbsapi/getpoint/","_blank");
        return false;
    })

});
var wait=60;
function time() {
    if (wait == 0) {
        $("#submitCode").attr("disabled", false)
        $("#submitCode").text("获取验证码");
        wait = 60;
    } else {
        $("#submitCode").attr("disabled", true);
        $("#submitCode").text("重新发送(" + wait + ")");
        wait--;
        setTimeout(function () {
                time()
            },
            1000)
    }
}



